package com.example.demo.pdf.service.impl;

import com.example.demo.config.ResourceProvider;
import com.example.demo.pdf.service.PdfGeneratorService;
import lombok.extern.slf4j.Slf4j;
import org.dromara.pdf.fop.handler.TemplateHandler;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.Map;

/**
 * @ClassName PdfGeneratorServiceImpl
 * @Description PDF生成服务实现类
 * @Author MingChang.Wei
 * @Date 2025/9/26
 **/
@Slf4j
@Service
public class PdfGeneratorServiceImpl implements PdfGeneratorService {

    @Resource
    private ResourceProvider resourceProvider;

    @Override
    public void generatePdfFromTemplate(String templateName, Map<String, Object> data, String outputPath) {
        try {
            String configPath = resourceProvider.getResourcePath("fop.xconf");
            String templatePath = resourceProvider.getResourceDir(templateName);
            
            log.info("开始生成PDF - 模板: {}, 输出路径: {}", templateName, outputPath);
            
            TemplateHandler.DataSource.Freemarker.setTemplatePath(templatePath);
            TemplateHandler.Template.build()
                    .setConfigPath(configPath)
                    .setDataSource(
                            TemplateHandler.DataSource.Freemarker.build()
                                    .setTemplateName(templateName)
                                    .setTemplateData(data)
                    )
                    .transform(outputPath);
                    
            log.info("PDF生成成功: {}", outputPath);
        } catch (Exception e) {
            log.error("PDF生成失败: {}", e.getMessage(), e);
            throw new RuntimeException("PDF生成失败: " + e.getMessage(), e);
        }
    }

    @Override
    public void generatePdfFromHtml(String htmlContent, String outputPath) {
        try {
            log.info("开始从HTML生成PDF - 输出路径: {}", outputPath);
            
            // 这里可以集成其他HTML转PDF的库，如iText、Flying Saucer等
            // 示例实现（需要根据实际使用的库进行调整）
            
            log.info("HTML转PDF生成成功: {}", outputPath);
        } catch (Exception e) {
            log.error("HTML转PDF失败: {}", e.getMessage(), e);
            throw new RuntimeException("HTML转PDF失败: " + e.getMessage(), e);
        }
    }

    @Override
    public void generatePdfFromText(String textContent, String outputPath) {
        try {
            log.info("开始从文本生成PDF - 输出路径: {}", outputPath);
            
            // 这里可以使用iText等库直接从文本生成PDF
            // 示例实现（需要根据实际使用的库进行调整）
            
            log.info("文本转PDF生成成功: {}", outputPath);
        } catch (Exception e) {
            log.error("文本转PDF失败: {}", e.getMessage(), e);
            throw new RuntimeException("文本转PDF失败: " + e.getMessage(), e);
        }
    }

    @Override
    public void generateReportPdf(Map<String, Object> reportData, String templateName, String outputPath) {
        try {
            log.info("开始生成报表PDF - 模板: {}, 输出路径: {}", templateName, outputPath);
            
            // 为报表数据添加一些通用字段
            reportData.put("generateTime", System.currentTimeMillis());
            reportData.put("reportTitle", reportData.getOrDefault("title", "系统报表"));
            
            // 使用模板生成PDF
            generatePdfFromTemplate(templateName, reportData, outputPath);
            
            log.info("报表PDF生成成功: {}", outputPath);
        } catch (Exception e) {
            log.error("报表PDF生成失败: {}", e.getMessage(), e);
            throw new RuntimeException("报表PDF生成失败: " + e.getMessage(), e);
        }
    }
}
